package com.zoz.service.impl;

import java.io.File;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zoz.constant.CommonConstant;
import com.zoz.service.CommonCodeFactoryEngineService;
import com.zoz.service.DatabaseService;
import com.zoz.utils.EngineUtil;
import com.zoz.utils.FileUtil;
import com.zoz.utils.GenCodeUtil;
import com.zoz.utils.ToCamelUtil;
import com.zoz.utils.ZipCompressorUtil;
import com.zoz.vo.EngineModelVo;
import com.zoz.vo.EngineVo;
/**
 * 引擎公共工厂
 *@author zhuhg
 *@date 2020年6月5日 下午1:32:30
 */

@Service
public class CommonCodeFactoryEngineServiceImpl implements CommonCodeFactoryEngineService {

	@Autowired
	private ResourceLoader resourceLoader;
	@Autowired
	private DatabaseService databaseService;
	@Override
	public String readEngineToJavaFile(EngineVo vo) throws Exception {
		JSONObject json = new JSONObject();
		Map<String,Object> enineMap = new HashMap<>();
		enineMap.put("package", vo.getPackName());
		enineMap.put("tableComment", vo.getTableComment());
		enineMap.put("tableName", vo.getTableName());
		enineMap.put("projectName", vo.getProjectName());
		enineMap.put("author", vo.getAuthor());
		enineMap.put("date", LocalDateTime.now().toString());
		enineMap.put("apiProject", EngineUtil.getApiProject(vo.getProjectName()));
		enineMap.put("columns", EngineUtil.getColumnList(vo.getColumns()));
		String className = ToCamelUtil.toCapitalizeCamelCase(vo.getTableName());
		enineMap.put("className", className);
		String changeClassName = ToCamelUtil.toCamelCase(vo.getTableName());
		enineMap.put("changeClassName", changeClassName);
		String path = resourceLoader.getResource("classpath:template").getFile().getPath()  +  File.separator;
		String main = vo.getProjectName() + File.separator +  "src"+ File.separator + "main" + File.separator + "java";
		String packs = path +  main  + File.separator + vo.getPackName().replace(".", File.separator);
		GenCodeUtil.codeFile(enineMap, EngineUtil.getTemplatePaths(JSON.parseArray(vo.getTemplates(), String.class), className, packs));
		json.put("status", CommonConstant.STATUS_Y);
		json.put("message","生成文件成功");
		return json.toJSONString();
	}
	@Override
	public void downloadFile(HttpServletRequest request, HttpServletResponse response, EngineVo vo) throws Exception {
		Map<String,Object> enineMap = new HashMap<>();
		enineMap.put("package", vo.getPackName());
		enineMap.put("tableComment", vo.getTableComment());
		enineMap.put("tableName", vo.getTableName());
		enineMap.put("projectName", vo.getProjectName());
		enineMap.put("author", vo.getAuthor());
		enineMap.put("date", LocalDateTime.now().toString());
		enineMap.put("apiProject", EngineUtil.getApiProject(vo.getProjectName()));
		enineMap.put("columns", EngineUtil.getColumnList(vo.getColumns()));
		String className = ToCamelUtil.toCapitalizeCamelCase(vo.getTableName());
		enineMap.put("className", className);
		String changeClassName = ToCamelUtil.toCamelCase(vo.getTableName());
		enineMap.put("changeClassName", changeClassName);
		String path = resourceLoader.getResource(CommonConstant.CLASSPATH_TEMPLATE).getFile().getPath()  +  File.separator;
		String main = vo.getProjectName() + File.separator +  "src"+ File.separator + "main" + File.separator + "java";
		String packs = path +  main  + File.separator + vo.getPackName().replace(".", File.separator);
		String readPath = resourceLoader.getResource(CommonConstant.CLASSPATH_TEMPLATE + File.separator + "zoz").getFile().getPath();
		if(!StringUtils.isEmpty(readPath)) {
			List<JSONObject> pathList= EngineUtil.getTemplatePaths(FileUtil.getPathFiles(readPath), className, packs);
			GenCodeUtil.codeFile(enineMap, pathList);
			path = path + vo.getProjectName();
			File zipFile = new File(path + ".zip");
			ZipCompressorUtil.compress(path, zipFile.getPath());
			FileUtil.downloadFile(request, response, zipFile);
			//删除生成的文件
			FileUtil.deleteFolder(path);
			FileUtil.deleteFolder(zipFile.getPath());
		}
		
	}
	@Override
	public void downloadMarryFile(HttpServletRequest request, HttpServletResponse response, EngineModelVo vo)
			throws Exception {
		// 数据库名称
		String database = vo.getDatabase();
		// 选择的表
		String tables = vo.getTables();
		List<JSONObject> tableList = JSON.parseArray(tables,JSONObject.class);
		// 循环利用
		for (JSONObject obj : tableList) {
			EngineVo engine = new EngineVo();
			engine.setAuthor(vo.getAuthor());
			engine.setPackName(vo.getPackName());
			engine.setProjectName(vo.getProjectName());
			// table_name,table_comment
			engine.setTableName(obj.getString("table_name"));
			engine.setTableComment(obj.getString("table_comment"));
			engine.setTemplates(vo.getTemplates());
			// 查询数据库
			String columns = databaseService.queryCurDatabaseTable(database, obj.getString("table_name"));
			engine.setColumns(columns);
			this.readEngineToJavaFile(engine);
		}
		String path = resourceLoader.getResource(CommonConstant.CLASSPATH_TEMPLATE).getFile().getPath()  +  File.separator;
		path = path + vo.getProjectName();
		File zipFile = new File(path + ".zip");
		ZipCompressorUtil.compress(path, zipFile.getPath());
		FileUtil.downloadFile(request, response, zipFile);
		//删除生成的文件
		FileUtil.deleteFolder(path);
		FileUtil.deleteFolder(zipFile.getPath());
	}
	
	
	
	

}
